<?php
    session_start();
	include_once 'config/common.php';
	include_once 'head.php';
	$action=$_GET['action'];
	$apic=new api();
	
	//获得市、区
	if($action=='getArea'){
		$child = $commonc->getArea($_POST['type'],$_POST['fatherid']);
		if($_POST['type']=='city'){
			$select = '<option value="">城市/地区</option>';
		}elseif($_POST['type']=='county'){
			$select = '<option value="">区/县</option>';
		}
		if(is_array($child)){
			$error="0";
			foreach($child as $k=>$v){
				$select.='<option value="'.$v[$_POST['type'].'id'].'">'.$v[$_POST['type']].'</option>';
			}
		}else{
			$error="1";
		}
		if($_POST['fatherid']=="") $error='2';

		$result = array('error'=>$error,'child'=>$select);
		echo json_encode($result);
		exit();
	}

	//点赞
	if($_GET['action']=='addZan'){
		$table="info_praise";		//点赞表
		$table_info="info";		   //资讯表

		$info_id=$_POST['info_id'];

		if($user_id!=''){
			$cache=$commonc->getInfo($table."  where info_id=".$info_id." and user_id=".$user_id);
			if($cache['id']>0){
				$message="请不要重复点赞";
			}else{
				//判断是否属于自己的文章
				$info=$commonc->getInfo($table_info." where id=".$info_id." and user_id=".$user_id." and type='user'");
				if($info['id']>0){
					$message="不能给自己点赞哦";
				}else{
					$arr=array('info_id'=>$info_id,'user_id'=>$user_id,'create_time'=>time());
					$commonc->insert($table,$arr);
					$message="成功点赞";
				}
			}
			$res=1;
		}else{
			$res=0;
		}
		$arr=array('res'=>$res,'mes'=>$message);
		echo json_encode($arr);
		exit();
	}

	//取消赞
	if($_GET['action']=='cutZan'){
		$table="info_praise";		//表名
		$info_id=$_POST['info_id'];
		if($user_id!=''){
			$cache=$commonc->getInfo($table."  where info_id=".$info_id." and user_id=".$user_id);
			if($cache['id']>0){
				$sql = "DELETE FROM ".$table." WHERE info_id='".$info_id."' AND user_id ='".$user_id."'";
				$commonc->query($sql);
				$message="取消成功";
			}else{
				$message="您还没有点赞过哦";
			}
			$res=1;
		}else{
			$res=0;
		}
		$arr=array('res'=>$res,'mes'=>$message);
		echo json_encode($arr);
		exit();
	}

	//取消关注
	if($_GET['action']=='cutFocus'){
		$table="user_fans";		//表名
		$id=$_POST['id'];
		if($id!=''){
			$cache=$commonc->getInfo($table." where id=".$id);
			if($cache['id']>0){
				$sql = "DELETE FROM ".$table." WHERE id='".$id."'";
				$commonc->query($sql);
				$message="取消成功";
			}else{
				$message="您还没有关注哦";
			}
			$res=1;
		}else{
			$res=0;
		}
		$arr=array('res'=>$res,'mes'=>$message);
		echo json_encode($arr);
		exit();
	}

	//得到洲下国家
	if($_GET['action']=='getCountry'){
		$continent_id=$_POST['continent_id'];			//洲id
		$table="country";								//国家表
		$sql=$table.' where father_id="'.$continent_id.'"';
		$list=$commonc->getlist($sql);
		$html='<option value="">国家</option>';
		if(is_array($list['result'])){
			foreach($list['result'] as $k=>$v){
				$html.='<option value="'.$v['id'].'">'.$v['country'].'</option>';
			}
		}
		echo $html;
		exit();
	}

	if($_GET['action']=='checkUserEmail'){
		$table='user';
		$email=$_POST['data'];
		$sql=$table.' where email="'.$email.'"';
		$info=$commonc->getInfo($sql);
		if($info['email']!=''){
			$res=1;
		}else{
			$res=0;
		}
		$result=array('res'=>$res);
		echo json_encode($result);
		exit();
	}

	if($_GET['action']=='checkUserPhone'){
		$table='user';
		$phone=$_POST['data'];
		$sql=$table.' where phone="'.$phone.'"';
		$info=$commonc->getInfo($sql);
		if($info['phone']!=''){
			$res=1;
		}else{
			$res=0;
		}
		$result=array('res'=>$res);
		echo json_encode($result);
		exit();
	}

  
	//发送短信
	if($_GET['action']=='postMobile'){
		session_start();
		//1.设置信息
		$phone=$_POST['phone'];
		$code = rand(100000,999999);
		$data ="您好！验证码是：" . $code ." 请尽快使用";
		$_SESSION['code'] = $code;
		//2.发送短信
		$result=$apic->send($phone,$data);
		$error=1;
		echo json_encode(array('error'=>$error,'result'=>$result));
		exit;
	}

?>